import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JTabbedPane;
import javax.swing.JToolTip;



/**
 * @author Shangru Li
 * the class is used to provide a tabbed 
 * it extends the JTabbedPane and incorporates with more function, such as each tab 
 * is with the "X" for close
 * The class between Group and Panel2A
 */

public class JClosableTabbedPane extends JTabbedPane implements MouseListener {

	private static final long serialVersionUID = 1L;
	private double scaleRatio = 0.3;   
    private HashMap<String, Component> maps = new HashMap<String, Component>();        
    public JClosableTabbedPane() { //create a constructor with mouse listener  
        super(); 
        this.setSize(500,300);
        this.add("blank ",new Panel2A());
            
        addMouseListener(this);   
    } 
    //override the original functions in Jtabbedpane
    public void addTab(String title, Component component) {   
        this.addTab(title, component, null);   
    }          
    public void addTab(String title, Component component, Icon extraIcon) {   
        super.addTab(title, new CloseTabIcon(extraIcon), component);   
    }          
    public void insertTab(String title, Icon icon, Component component, String tip, int index) {   
        tip = "tab" + component.hashCode();   
        maps.put(tip, component);   
        super.insertTab(title, icon, component, tip, index);   
    }         
    public void removeTabAt(int index) {   
        Component component = getComponentAt(index);   
        maps.remove("tab" + component.hashCode());   
        super.removeTabAt(index);   
    }         
    public JToolTip createToolTip() {   
        ImageToolTip tooltip = new ImageToolTip();   
        tooltip.setComponent(this);   
        return tooltip;   
    }  

//       this class is used for create the picture for showing each tab when the mouse is on the tab title
        
    class ImageToolTip extends JToolTip {  
        public Dimension getPreferredSize() {   
            String tip = getTipText();   
            Component component = maps.get(tip);   
            if (component != null) {   
                return new Dimension((int) (getScaleRatio() * component.getWidth()), (int) (getScaleRatio() * component.getHeight()));   
            } else {   
                return super.getPreferredSize();   
            }   
        }               
//            public void paintComponent(Graphics g) { // draw a picture of each tab 
//                String tip = getTipText();   
//                Component component = maps.get(tip);   
//                if (component instanceof JComponent) {   
//                    JComponent jcomponent = (JComponent) component;   
//                    Graphics2D g2d = (Graphics2D) g;   
//                    AffineTransform at = g2d.getTransform();   
//                    g2d.transform(AffineTransform.getScaleInstance(getScaleRatio(), getScaleRatio()));   
//                    ArrayList<JComponent> dbcomponents = new ArrayList<JComponent>();   
//                    updateDoubleBuffered(jcomponent, dbcomponents);   
//                    jcomponent.paint(g);   
//                    resetDoubleBuffered(dbcomponents);   
//                    g2d.setTransform(at);   
//                }   
//            }  
            
        private void updateDoubleBuffered(JComponent component, ArrayList<JComponent> dbcomponents) {   
            if (component.isDoubleBuffered()) {   
                dbcomponents.add(component);   
                component.setDoubleBuffered(false);   
            }   
            for (int i = 0; i < component.getComponentCount(); i++) {   
                Component c = component.getComponent(i);   
                if (c instanceof JComponent) {   
                    updateDoubleBuffered((JComponent) c, dbcomponents);   
                }   
            }   
        }            
        private void resetDoubleBuffered(ArrayList<JComponent> dbcomponents) {   
            for (JComponent component : dbcomponents) {   
                component.setDoubleBuffered(true);   
            }   
        }   
    }  
      
    public double getScaleRatio() {  //get the scaleRatio 
        return scaleRatio;   
    } 
        
    public void setScaleRatio(double scaleRatio) {  //set the scaleRatio 
 
        this.scaleRatio = scaleRatio;   
    }
       
    public void mouseClicked(MouseEvent e) { 
        int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY()); 
            
        if (tabNumber < 0) {   
            return;   
        }        
        Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds(); //get the clicked coordinates  
        if (rect.contains(e.getX(), e.getY())) { // if the clicked one is the "X" of any tab
            	
      	    ((Panel2A) getComponentAt (getSelectedIndex())).fileAction(4);
      	    if(!((Panel2A) getComponentAt (getSelectedIndex())).cancel){
                this.removeTabAt(getSelectedIndex()); // the tab will be deleted
            }
            	 
            	
            	
        }
        	
        	
    }  
        
    public void mouseEntered(MouseEvent e) {}   
    public void mouseExited(MouseEvent e) {}   
    public void mousePressed(MouseEvent e) {}   
    public void mouseReleased(MouseEvent e) {}   
    }